package jzqb.controller.admin;


import haha.life.common.CutPageBean;
import haha.life.common.PermissionTypeTool;
import haha.life.common.SysTool;
import haha.life.normal.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jzqb.annotation.PermissionCheck.PermissionCheck;
import jzqb.bo.NoticeBo;
import jzqb.common.Result;
import jzqb.po.NoticePo;
import jzqb.service.INoticeService;
import jzqb.service.MonitorService;
import jzqb.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Description:
 *
 * @author BaiGe
 * @date: 2021/4/7 16:14
 * @Version 1.0
 */

@Api(tags = "通知公告-后台")
@RestController
@RequestMapping("/admin/notice")
public class AdminNoticeController {

    @Autowired
    private INoticeService noticeService;


    @PermissionCheck(permissionTypeList = {PermissionTypeTool.business_mgt})
    @ApiOperation(value = "查询通知公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "searchValue", value = "搜索内容", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "当前页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/queryNotice")
    public Result queryNotice(@RequestParam(value = "startTime", required = false) String startTime,
                              @RequestParam(value = "endTime", required = false) String endTime,
                              @RequestParam(value = "searchValue", required = false) String searchValue,
                              @RequestParam("page") Integer page,
                              @RequestParam("pageSize") Integer pageSize
    ) {
        NoticeBo noticeBo = new NoticeBo();
        noticeBo.setSearchValue(searchValue);
        noticeBo.setStartTime(startTime);
        noticeBo.setEndTime(endTime);
        noticeBo.setPage(page);
        noticeBo.setPageSize(pageSize);
        noticeBo.setStart(page, pageSize);

        CutPageBean<NoticeVo> cutPageBean = noticeService.showNotice(noticeBo);
        return Result.succ(cutPageBean);
    }

    @PermissionCheck(permissionTypeList = {PermissionTypeTool.business_mgt})
    @ApiOperation(value = "新建通知公告")
    @PostMapping(value = "/insertNotice")
    public Result insertNotice(@RequestBody NoticePo noticePo, HttpServletRequest request) {
        //解析token获取id
        String userID = TokenUtils.getCurrentUserId(request);

        noticePo.setSenderId(userID);
        noticePo.setSendTime(new SimpleDateFormat(SysTool.dateFormat_milli).format(new Date()));

//
//        MonitorPo po = new MonitorPo();
//        po.setOperateType(OperateEnum.addUser.ordinal());
//        po.setUserid(userID);
//        po.setArg0("dddd");
//        Boolean sss = monitorService.record(po);


        Boolean result = noticeService.insertNotice(noticePo);
        if (result) {
            return Result.succ("添加成功！");
        } else {
            return Result.fail("添加失败");
        }
    }

    @PermissionCheck(permissionTypeList = {PermissionTypeTool.business_mgt})
    @ApiOperation(value = "删除通知公告")
    @PostMapping(value = "/delNotice")
    public Result delNotice(@RequestBody NoticePo noticePo) {

        Boolean result = noticeService.delNotice(noticePo.getId());
        if (result) {
            return Result.succ("删除成功！");
        } else {
            return Result.fail("删除失败");
        }
    }

    @PermissionCheck(permissionTypeList = {PermissionTypeTool.business_mgt})
    @ApiOperation(value = "编辑通知公告")
    @PostMapping(value = "/updateNotice")
    public Result updateNotice(@RequestBody NoticePo noticePo) {
        Boolean result = noticeService.updateNotice(noticePo);
        if (result) {
            return Result.succ("修改成功！");
        } else {
            return Result.fail("修改失败！");
        }
    }


    @ApiOperation(value = "获取分类领域")
    @GetMapping(value = "/getNoticeSort")
    public Result getNoticeSort() {
        List<NoticeVo> noticeSort = noticeService.getNoticeSort();
        return Result.succ(noticeSort);

    }
}
